package com.wwg.market.ui.code.client.widgets;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.logical.shared.SelectionEvent;
import com.google.gwt.event.logical.shared.SelectionHandler;
import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.mobileares.midp.mui.client.table.UFishTable;
import com.mobileares.midp.widgets.client.anchor.TextAnchor;
import com.mobileares.midp.widgets.client.menu.PopMenu;
import com.mobileares.midp.widgets.client.pop.IMessageConfirm;
import com.mobileares.midp.widgets.client.pop.Message;
import com.nexustar.gwt.widgets.client.asyn.LoadingAsyncCallback;
import com.nexustar.gwt.widgets.client.ui.table.*;
import com.wwg.market.ui.code.client.CodeFormFactory;
import com.wwg.market.ui.code.client.CodePageItem;
import com.wwg.market.ui.dashboard.client.Utils;
import com.wwg.market.ui.service.dto.GeneralCodeDto;
import com.wwg.market.ui.service.interfaces.CodeService;

import java.util.ArrayList;
import java.util.Iterator;

/**
 * Created by IntelliJ IDEA.
 * User: Tom
 * Date: 2011-12-27
 * Time: 16:08:10
 * To change this template use File | Settings | File Templates.
 */
public class CodeListWidget extends Composite {
    private int width = 300;
    private int height = 300;
    //    private HorizontalPanel container;
    private PopMenu popMenu;
    private UFishTable<GeneralCodeDto> listWidget;
    private TextAnchor moveConfirm;
    private TextAnchor moveCancel;


    public CodeListWidget(CodePageItem page) {
        init();
        initWidget(listWidget);
        initConfirmEvent();
    }

    private void initConfirmEvent() {
        moveConfirm = new TextAnchor("确定");
        moveConfirm.addClickHandler(new ClickHandler() {
            public void onClick(ClickEvent clickEvent) {
                Utils.invoke(CodeService.class.getName(), "changeOrder",
                        new Object[]{getListWidgetModel()}, new LoadingAsyncCallback() {
                            @Override
                            protected void success(Object o) {
                                moveConfirm.removeFromParent();
                                moveCancel.removeFromParent();
                                listWidget.getFishModel().load((java.util.List) o);
                            }
                        });
            }
        });
        moveCancel = new TextAnchor("取消");
        moveCancel.addClickHandler(new ClickHandler() {
            public void onClick(ClickEvent clickEvent) {
                moveConfirm.removeFromParent();
                moveCancel.removeFromParent();
                listWidget.getFishModel().render();
            }
        });
    }

    private java.util.List getListWidgetModel() {
        java.util.List result = new ArrayList();
        if (listWidget.getBMUI().getTableItemCount() > 0) {
            for (Iterator it = listWidget.getBMUI().getTableItems(); it.hasNext();) {
                TableItem item = (TableItem) it.next();
                result.add(item.getUserObject());
            }
        }
        return result;
    }

    private void init() {
        popMenu = new PopMenu();
        popMenu.addMenuItem("上移", new Command() {
            public void execute() {
                int index = listWidget.getBMUI().getTableItemIndex(listWidget.getBMUI().getSelectedItem());
                if (index > 0) {
                    TableItem item = listWidget.getBMUI().getSelectedItem();
                    item.removeFromParent();
                    listWidget.getBMUI().insertTableItem(item, index - 1);
                    CellItem cell = (CellItem) item.getWidget(0, 3);
                    HorizontalPanel hp = (HorizontalPanel) cell.getWidget();

                    if (hp.getWidgetIndex(moveConfirm) == -1) {
                        hp.add(moveConfirm);
                        hp.add(moveCancel);
                    }
                }
            }
        });
        popMenu.addMenuItem("下移", new Command() {
            public void execute() {
                int index = listWidget.getBMUI().getTableItemIndex(listWidget.getBMUI().getSelectedItem());
                if (index < listWidget.getBMUI().getTableItemCount() - 1) {
                    TableItem item = listWidget.getBMUI().getSelectedItem();
                    item.removeFromParent();
                    listWidget.getBMUI().insertTableItem(item, index + 1);
                    CellItem cell = (CellItem) item.getWidget(0, 3);
                    HorizontalPanel hp = (HorizontalPanel) cell.getWidget();


                    if (hp.getWidgetIndex(moveConfirm) == -1) {
                        hp.add(moveConfirm);
                        hp.add(moveCancel);
                    }
                }
            }
        });

        popMenu.addMenuItem("修改", new Command() {
            public void execute() {
                CodeFormFactory.getEditCodeForm().setModel(listWidget.getFishModel());
                CodeFormFactory.getEditCodeForm().show((GeneralCodeDto) listWidget.getBMUI().getSelectedItem().getUserObject());
            }
        });
        popMenu.addMenuItem("删除", new Command() {
            public void execute() {
                Message.showConfirm("温馨提示", "是否要删除当前类别?", new IMessageConfirm() {
                    public void fire(boolean confirm) {
                        if (confirm) {
                            final GeneralCodeDto dto = (GeneralCodeDto) listWidget.getBMUI().getSelectedItem().getUserObject();
                            Utils.invoke(CodeService.class.getName(), "delete", new Object[]{dto}, new LoadingAsyncCallback() {
                                @Override
                                protected void success(Object o) {
                                    listWidget.getFishModel().deleteItem(dto);
                                }
                            });
                        }
                    }
                });
            }
        });

        listWidget = getUFishListWidget();
    }

//    public void setSelectionHandler(final SelectionHandler<TableItem> handler) {
//        listWidget.getBMUI().addSelectionHandler(new SelectionHandler<TableItem>() {
//            public void onSelection(SelectionEvent<TableItem> tableItemSelectionEvent) {
//                handler.onSelection(tableItemSelectionEvent);
//            }
//        });
//    }

    private UFishTable<GeneralCodeDto> getUFishListWidget() {
        ViewTable table = new ViewTable(getCW());
        UFishTable<GeneralCodeDto> list = new UFishTable(table);
        list.getBMUI().setWidth(width + "px");
        list.getBMUI().setHeight(height + "px");
        return list;
    }

    private ColumnWaiter getCW() {
        ColumnExt c1 = new ColumnExt("代码名称", 100, new IColumnRender() {
            public void renderColumn(CellItem cellItem) {
                GeneralCodeDto dto = (GeneralCodeDto) cellItem.getUserObject();
                cellItem.setText(dto.getName());
            }
        });
        ColumnExt c2 = new ColumnExt("代码值", 100, new IColumnRender() {
            public void renderColumn(CellItem cellItem) {
                GeneralCodeDto dto = (GeneralCodeDto) cellItem.getUserObject();
                cellItem.setText(dto.getCode());
            }
        });
        ColumnExt c3 = new ColumnExt("操作", 150, new IColumnRender() {
            public void renderColumn(CellItem cellItem) {
//                final GeneralCodeDto dto = (GeneralCodeDto) cellItem.getUserObject();
                HorizontalPanel container = new HorizontalPanel();
                final TextAnchor oper = new TextAnchor("操作");
                oper.addClickHandler(new ClickHandler() {
                    public void onClick(ClickEvent clickEvent) {
                        int x = oper.getAbsoluteLeft();
                        int y = oper.getAbsoluteTop();
                        popMenu.show(x, y + 10);
                    }
                });
//
                container.add(oper);
                cellItem.setWidget(container);
            }
        });
        return new ColumnWaiter(new ColumnExt[]{c1, c2, c3});
    }

    public void render() {
        listWidget.getFishModel().render(listWidget.getFishWidget());
    }

    public UFishTable<GeneralCodeDto> getListWidget() {
        return listWidget;
    }

}
